package com.wh.TestSpinlock;

import java.util.concurrent.atomic.AtomicReference;

public class SpinlockDemo1 {

    AtomicReference<Thread> atomicReference = new AtomicReference<>();

    public void myLock() {
        Thread thread = Thread.currentThread();

        //自旋锁  while 和 do while 一样
        while (!atomicReference.compareAndSet(null, thread)) {
            System.out.print(thread.getName()+"---自旋");
        }
        System.out.println(thread.currentThread().getName() + "-->mylock");
        System.out.println();
    }

    public void myUnLock() {
        Thread thread = Thread.currentThread();
        atomicReference.compareAndSet(thread, null);
        System.out.println();
        System.out.println(thread.currentThread().getName() + "===>myUnlock");
    }
}
